import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LoginUI extends JFrame {
    private String userId;
    private String publicKeyStr;
    private String privateKeyStr;
    private JTextField userIdField;       // 用户ID输入框
    private JTextField publicKeyField;    // 用户公钥输入框
    private JPasswordField privateKeyField; // 用户私钥输入框
    private JButton loginButton;          // 登录按钮
    private JButton exitButton;           // 退出按钮
    public List<Map.Entry<String, String>> shareDataList; // 在各个界面中传递，实现数据互通

    public LoginUI(String userId,String publicKeyStr, String privateKeyStr) {
        if(userId!=null && publicKeyStr!=null && privateKeyStr!=null){
            this.userId = userId;
            this.publicKeyStr = publicKeyStr;
            this.privateKeyStr = privateKeyStr;
        }
        // 设置窗体的标题
        setTitle("登录");

        // 设置窗体的大小
        setSize(300, 300);
        // 关闭窗体时退出程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 使用 BoxLayout 进行垂直布局
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        // 创建输入框面板
        JPanel userIdPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 左对齐
        JLabel userIdLabel = new JLabel("用户 ID : ");
        userIdField = new JTextField(20);  // 设置输入框宽度为20个字符
        if(userId!=null){
            userIdField.setText(userId);
        }
        userIdPanel.add(userIdLabel);
        userIdPanel.add(userIdField);

        JPanel publicKeyPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 左对齐
        JLabel publicKeyLabel = new JLabel("用户公钥:");
        publicKeyField = new JTextField(20);
        if(publicKeyStr!=null){
            publicKeyField.setText(publicKeyStr);
        }
        publicKeyPanel.add(publicKeyLabel);
        publicKeyPanel.add(publicKeyField);

        JPanel privateKeyPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 左对齐
        JLabel privateKeyLabel = new JLabel("用户私钥:");
        privateKeyField = new JPasswordField(20);
        if(privateKeyStr!=null){
            privateKeyField.setText(privateKeyStr);
        }
        privateKeyPanel.add(privateKeyLabel);
        privateKeyPanel.add(privateKeyField);

        // 创建按钮
        loginButton = new JButton("登录");
        exitButton = new JButton("退出");

        // 登录按钮事件监听器
        this.shareDataList = new ArrayList<Map.Entry<String, String>>();
        userId = userIdField.getText();
        publicKeyStr = publicKeyField.getText();
        privateKeyStr = new String(privateKeyField.getPassword());

        Map.Entry<String, String> userIdEntry = Map.entry("userId", userId);
        Map.Entry<String, String> publicKeyEntry = Map.entry("publicKey", publicKeyStr);
        Map.Entry<String, String> privateKeyEntry = Map.entry("privateKey", privateKeyStr);
        shareDataList.add(userIdEntry);
        shareDataList.add(publicKeyEntry);
        shareDataList.add(privateKeyEntry);

        String finalUserId = userId;
        String finalPublicKeyStr = publicKeyStr;
        String finalPrivateKeyStr = privateKeyStr;
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if (LoginValid(finalUserId, finalPublicKeyStr, finalPrivateKeyStr)) {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                LoginUI.this.dispose(); // 关闭登录界面、开启主界面
                                MainUI mainUi = new MainUI(shareDataList);
                                mainUi.setVisible(true);
                            }
                        });
                    } else {
                        System.out.println("用户键入的公私钥对异常");
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        // 退出按钮事件监听器
        exitButton.addActionListener(e -> System.exit(0));

        // 创建面板来水平放置两个按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(loginButton);
        buttonPanel.add(exitButton);

        // 添加组件到窗体
        add(userIdPanel);
        add(publicKeyPanel);
        add(privateKeyPanel);
        add(buttonPanel);  // 将按钮面板添加到窗口

        // 设置窗体居中显示
        setLocationRelativeTo(null);
    }

    private boolean LoginValid(String userId, String publicKey, String privateKey) throws Exception {
//        System.out.println("userId: "+userId);
//        System.out.println("pubKey: "+publicKey);
//        System.out.println("priKey: "+privateKey);
        if (userId.length() == 0 || publicKey.length() == 0 || privateKey.length() == 0) {
            return false;
        }
        byte[] signature = ECCKeyGen.sign(privateKey, userId);
        boolean flag = ECCKeyGen.verify(publicKey,userId,signature);
        return flag;
    }

    public static void main(String[] args) {
        // 在事件调度线程中启动应用程序
        String publicKeyStr = "123";
        String privateKeyStr = "321";
        String userId = "213";
        SwingUtilities.invokeLater(() -> new LoginUI(userId,publicKeyStr, privateKeyStr).setVisible(true));
    }
}
